<?php 
if(!isset($_SESSION)) 
{ 
session_start(); 
} 
include '../connection/connection_screen.php';
try{
	if($connection_screen){
		include 'showscreen.php';
		set_time_limit(0);
		$idmonitor = $_REQUEST['monitorid'];
		$winW = $_REQUEST['winW'];
		$winH = $_REQUEST['winH'];
		$bd=0;
		$kt=64;
		$cot=1;
		$vmaxnum=$numb = 64;
		$query1 = 'SELECT nCamera FROM monitor m WHERE m.Monitor_ID='.$idmonitor;
		$result = mysql_query($query1,$connection_screen);
		while( $row=mysql_fetch_array($result) ){
			$cot=$row['nCamera']+1;
			$numb=$cot * $cot;
			$kt = $numb;
		}
		mysql_free_result($result);
		$onewidth=($winW-30)/$cot;
		$oneheight=($winH-120)/$cot;
		$query = 'SELECT cardis.`host`,cardis.filename,cardis.car_datetime,cardis.id,cardis.`name`,fa.pos as posfav,confc.camera_type,confc.lens_type,confc.trigger_type,mn.nCamera as slCamera,mn.timeOut FROM monitor mn, favourite fa,
			 cardisplay cardis,configcamera confc 
			WHERE mn.Monitor_ID = fa.monitor_ID AND cardis.id = confc.id 
			AND fa.camera_ID = cardis.id AND mn.Monitor_ID = '.$idmonitor.' ORDER BY fa.pos ASC';
		$selectpos = mysql_query($query,$connection_screen);
		$mang=array();
		for($i=0;$i<$vmaxnum;$i++){
			$objj= new showscreen();
			$mang[$i+1]=$objj;
		}
		$_SESSION['mcar']=array();
		while( $row=mysql_fetch_array($selectpos) ){
			$ob= new showscreen();
			$ob->Instance(chop($row['posfav']), chop($row['filename']), chop($row['host']), chop($row['car_datetime']), chop($row['camera_type']), chop($row['lens_type']), chop($row['name']), chop($row['id']), chop($row['trigger_type']), chop($row['timeOut']));
			$mang[$row['posfav']]=$ob;
		}
		mysql_free_result($selectpos);
		$_SESSION['mcar']=$mang;
		mysql_close($connection_screen);
		$mfont=($onewidth / 21)."px";
		?>
		<div>
		<div id="inputs"><input type="hidden" id="somanhinh" name="somanhinh" value="<?=$numb?>" style="display:none"/></div>
		<div id="tables">
			<table width="100%" height="88%" border="0" style="background:none; cellsapcing:5px;;border-collapse: separate;" onmousedown="return false" onselectstart='return false;'>
				<?php 
					if(!empty($mang)){
						$vitri=0;
						for($i=$bd;$i<$kt;$i++){
							$CT = unserialize (serialize ($_SESSION['mcar'][$i+1]));
							if($vitri%$cot==0){
								?>
								<tr>
								<?php 
							}
								?>
								<td style="background-size:<?php echo $onewidth."px"?> <?php echo $oneheight."px"?>;background-repeat:no-repeat;padding-bottom: 0.1%">
									<div  id="<?php echo 'hinh'.($i+1)?>" ondblclick="show_prenext_car(<?=$numb?>,<?=$idmonitor?>,<?=$CT->id?>,<?=$bd?>,<?=$kt?>,<?=($i+1)?>);" style="border-bottom-style: hidden;width:<?php echo $onewidth."px"?>;height:<?php echo $oneheight."px"?>;position:relative;">
										<div style="position:absolute;">
											<?php 
											if($CT->pos==0){
												?>
												<img src="images/noimage.jpg" style='width:<?php echo $onewidth."px"?>;height:<?php echo $oneheight."px"?>;border: 1px solid #CFD6D9;border-radius: 2px;' onerror="this.src='images/noimage.jpg';" />
												<?php 
											}else{
												?>
												<img src="<?=$CT->image?>" style='width:<?php echo $onewidth."px"?>;height:<?php echo $oneheight."px"?>;border: 1px solid #CFD6D9;border-radius: 2px;' onerror="this.src='images/noimage.jpg';" />
												<?php 
											}
											?>
										</div>
										<div style="position:absolute;">
										<table style="border-bottom-style: hidden;background:none; padding:1%;width:<?php echo $onewidth."px"?>;height:<?php echo $oneheight."px"?>;" border="0">
											<tr >
												<td valign="top" >
													<table width="100%" border="0" style="background:none;border-bottom-style: hidden; ">
														<tr>
															<td align="left" valign="top"  style="color: yellow;font-size:<?php echo $mfont;?>;"><?php echo $CT->name?></td>
															<td align="right" valign="top">
																<?php if ($CT->light == 1) {?>
												              	<img src='images/circle-green.png' style='width:13px;height:13px;'>
												              	<?php }else if ($CT->light == 0){?>
												              		<img src='images/circle_red.png' style='width:13px;height:13px;'>
												              <?php }?>
															</td>
														</tr>
													</table>
												</td>
											<tr>
											<tr>
												<td valign="bottom">
													<table  width="100%"  border="0"  style="background:none;border-bottom-style: hidden; ">
														<tr >
															<td colspan="2" align="right"   style="color: yellow;font-size:<?php echo $mfont;?>;"><?=$CT->distance?></td>
														</tr>
														<tr>
															<td align="left"  style="color: yellow;font-size:<?php echo $mfont;?>;"><?=$CT->car_datetime?></td>
															<td align="right"  style="color: yellow;font-size:<?php echo $mfont;?>;"><?=$CT->vstriger_type?></td>
														<tr>
														<tr valign="bottom" >
															<td align="left"  style="color: yellow;font-size:<?php echo $mfont;?>;"><?=$CT->platecar?></td>
															<td align="right"  style="color: yellow;font-size:<?php echo $mfont;?>;">
																<?php 
													              	if($CT->pos==0){
													              		echo ($i+1);
													              	}else{
													              		echo $CT->pos;	
													              	}
													           ?>
															</td>
														<tr>
													</table>
												</td>
											</tr>
										</table>
										</div>
									</div>
								</td>
								<?php 
							$vitri++;
							if($vitri%$cot==0){
								?>
								</tr>
								<?php 
							}
						}
					}
				?>
			</table></div>
		</div>
		<?php 
	}else{
		echo "died";	
	}
}catch(Exception $e){
	echo $e->getMessage();
}
?>
